package com.itheima.service.impl.oms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.oms.OmsOrderReturnApplyMapper;
import com.itheima.pojo.oms.OmsOrderReturnApply;
import com.itheima.pojo.oms.statusParam;
import com.itheima.service.oms.OmsOrderReturnApplyService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply> implements OmsOrderReturnApplyService {

    /*分页查询退货申请*/
    @Override
    public PageInfo listAll(int  pageNum, int  pageSize,String createTime,String handleMan,
                            String handleTime,Integer id,String receiverKeyword,Integer status) {

        Page<OmsOrderReturnApply> objectPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(createTime!=null,OmsOrderReturnApply::getCreateTime,createTime)
                .like(handleMan!=null,OmsOrderReturnApply::getHandleMan,handleMan)
                .like(handleTime!=null,OmsOrderReturnApply::getHandleTime,handleTime)
                .like(id!=null,OmsOrderReturnApply::getId,id)
                .like(receiverKeyword!=null,OmsOrderReturnApply::getReturnName,receiverKeyword)
                .like(receiverKeyword!=null,OmsOrderReturnApply::getReturnPhone,receiverKeyword)
                .like(status!=null,OmsOrderReturnApply::getStatus,status)
                .orderByAsc(OmsOrderReturnApply::getId);
        Page<OmsOrderReturnApply> page = this.page(objectPage, queryWrapper);
        List<OmsOrderReturnApply> records = page.getRecords();
        int total = (int)page.getTotal();
        int current = (int)page.getCurrent();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(records);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalPage(current);
        return pageInfo;
    }

    /* 修改退货申请状态*/
    @Override
    public void updatestatus(@RequestParam int id, @RequestBody statusParam statusParam) {
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrderReturnApply::getId,id);

        List<OmsOrderReturnApply> list = this.list(queryWrapper);
        List<OmsOrderReturnApply> list1 = list.stream().map(item -> {
            item.setCompanyAddressId(statusParam.getCompanyAddressId().longValue());
            item.setHandleMan(statusParam.getHandleMan());
            item.setHandleNote(statusParam.getHandleNote());
            //item.setProductId(statusParam.getId().longValue());
            item.setReceiveMan(statusParam.getReceiveMan());
            item.setReceiveNote(statusParam.getReceiveNote());
            item.setReturnAmount(new BigDecimal(statusParam.getReturnAmount().doubleValue()));
            item.setStatus(statusParam.getStatus());
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(list1);
    }


}
